package com.tf.integrated.controller;

import com.alibaba.fastjson.JSONObject;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.config.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 日志管理接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "日志管理接口",description="DeviceLogController")
@RestController
public class DeviceLogController extends BaseController
{
    @ApiOperation("查询日志")
    @GetMapping("/log/")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSONObject getLog(@RequestParam Map<String, String> data) {
        String disUrl="/log/?";
        if(data!=null&&data.size()>0&&isExist(data.get("host"))) {
            String host = data.get("host");
            disUrl=disUrl+Config.buildMap(data);
            HttpMethod method = HttpMethod.GET;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            String analyseResults = Config.client(Config.getStarturlpre(host) +disUrl, method, null, headers);
            LogController.add(disUrl,LogController.METHOD_GET,1);
            return JSONObject.parseObject(analyseResults);
        }
        LogController.add(disUrl,LogController.METHOD_GET,0);
        return new JSONObject();
    }

    @ApiOperation("设置当前配置")
    @PutMapping("/log/clear")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public JSONObject clearLog(@RequestParam Map<String, String> data) {
        String disUrl="/log/clear?";
        if(data!=null&&data.size()>0&&isExist(data.get("host"))) {
            String host = data.get("host");
            disUrl=disUrl+Config.buildMap(data);
            HttpMethod method = HttpMethod.PUT;
            HttpHeaders headers = new HttpHeaders();
            headers.set("X-Token", Config.getmCurtoken(host));
            logger.error("size"+Config.getDevUrlMap().size()+"  "+Config.getmDevToken().size());
            String analyseResults = Config.client(Config.getStarturlpre(host) +disUrl, method, null, headers);
            LogController.add(disUrl,LogController.METHOD_PUT,1);
            return JSONObject.parseObject(analyseResults);
        }
        LogController.add(disUrl,LogController.METHOD_PUT,0);
        return new JSONObject();
    }

    public boolean isExist(String value){
        return value != null && !value.equals("");
    }
}